using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Spec.Dto;
using JTZY.Model.Spec;
using JTZY.Repository;
using JTZY.Service.Spec.ISpecService;

namespace JTZY.Service.Spec
{
    /// <summary>
    /// 商品类型属性表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsgoodstypespecService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsgoodstypespecService : BaseService<Corecmsgoodstypespec>, ICorecmsgoodstypespecService
    {
        /// <summary>
        /// 查询商品类型属性表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsgoodstypespecDto> GetList(CorecmsgoodstypespecQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Includes(x => x.CorecmsgoodstypespecvalueNav) //填充子对象
                .Where(predicate.ToExpression())
                .ToPage<Corecmsgoodstypespec, CorecmsgoodstypespecDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsgoodstypespec GetInfo(int Id)
        {
            var response = Queryable()
                .Includes(x => x.CorecmsgoodstypespecvalueNav) //填充子对象
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加商品类型属性表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsgoodstypespec AddCorecmsgoodstypespec(Corecmsgoodstypespec model)
        {
            return Context.InsertNav(model).Include(s1 => s1.CorecmsgoodstypespecvalueNav).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改商品类型属性表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsgoodstypespec(Corecmsgoodstypespec model)
        {
            return Context.UpdateNav(model).Include(z1 => z1.CorecmsgoodstypespecvalueNav).ExecuteCommand() ? 1 : 0;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsgoodstypespec> QueryExp(CorecmsgoodstypespecQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsgoodstypespec>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Name), it => it.Name == parm.Name);
            return predicate;
        }
    }
}